package com.nuo.backend.common.utils;

/**
 * @author lcc
 * @create 2021-05-28
 * @注意 本内容仅限于dev414内部传阅，禁止外泄以及用于其他的商业目的
 */

import cn.hutool.core.bean.copier.CopyOptions;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class BeanUtil {

    public static <A, B> B beanAtoBeanB(A beanA, Class<B> clazz, String... ignoreProperties) {
        try {
            B b = clazz.newInstance();
            cn.hutool.core.bean.BeanUtil.copyProperties(beanA, b, CopyOptions.create().setIgnoreProperties(ignoreProperties).ignoreError().ignoreNullValue());
            return b;
        } catch (Exception e) {
            e.printStackTrace();
            return (B) new Object();
        }
    }

    public static <A, B> List<B> listAtoListB(Collection<A> listA, Class<B> clazz, String... ignoreProperties) {
        List<B> listB = new ArrayList();
        if (!ObjectUtils.isEmpty(listA)) {
            try {
                for (A a : listA) {
                    listB.add(beanAtoBeanB(a, clazz, ignoreProperties));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return listB;
    }
}
